package com.innovation.sys.common.utils;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonUtils {

	private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);

	static {
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
	}

	public static String parseToString(Object object) {
		if (object == null) {
			return null;
		}
		try {
			return JSON.toJSONString(object, SerializerFeature.WriteDateUseDateFormat);
		} catch (Exception e) {
			logger.error("error:{}", e);
		}
		return null;
	}

	public static Map<String, Object> convertJsonStrToMap(String jsonStr) {
		Map<String, Object> map = JSON.parseObject(jsonStr, new TypeReference<Map<String, Object>>() {
		});
		return map;
	}

	public static String parseToStringWithType(Object object) {
		if (object == null) {
			return null;
		}
		try {
			return JSON.toJSONString(object, SerializerFeature.WriteDateUseDateFormat,
					SerializerFeature.WriteClassName);
		} catch (Exception e) {
			logger.error("error:{}", e);
		}
		return null;
	}
}
